package ac.beiwo.baas.admin.controller;

import ac.beiwo.baas.admin.entity.App;
import ac.beiwo.baas.admin.entity.dto.AppExport;
import ac.beiwo.baas.admin.service.AppService;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.common.entity.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static ac.beiwo.baas.common.entity.SimpleResult.success;

/**
 * 应用控制器
 * Created by Staryet on 15/9/17.
 */
@RestController
@RequestMapping(value = "/api/admin/app")
public class AppController {

    @Autowired
    private AppService appService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    public SimpleResult insert(@RequestBody App app) throws SimpleError {
        App newApp = appService.insert(app);
        SimpleResult result = success();
        result.putData("app", newApp);
        return result;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public SimpleResult delete(@PathVariable String id) throws SimpleError {
        appService.delete(id);
        return success();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public App get(@PathVariable String id) throws SimpleError {
        return appService.get(id);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public List<App> list() {
        return appService.list();
    }

    @RequestMapping(value = "/{id}/resetKey", method = RequestMethod.POST)
    @ResponseBody
    public SimpleResult resetKey(@PathVariable String id) throws SimpleError {
        appService.resetKey(id);
        return success();
    }

    @RequestMapping(value = "/{id}/resetMasterKey", method = RequestMethod.POST)
    @ResponseBody
    public SimpleResult resetMasterKey(@PathVariable String id) throws SimpleError {
        appService.resetMasterKey(id);
        return success();
    }

    @RequestMapping(value = "/{id}/export", method = RequestMethod.GET)
    @ResponseBody
    public AppExport export(@PathVariable String id) throws SimpleError {
        return appService.export(id);
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ResponseBody
    public SimpleResult importData(@RequestBody AppExport appExport) throws SimpleError {
        appService.importData(appExport);
        return success();
    }

}
